import 'package:flutter/material.dart';
import '../daylight_saving_page.dart';
import 'route_models.dart';

class AppRoutes {
  // Route names as constants
  static const String home = '/';
  static const String daylightSaving = '/daylightSaving';

  // Route configuration
  static Map<String, WidgetBuilder> getRoutes(RouteSettings settings) {
    return {
      home: (context) => settings.arguments as Widget,
      daylightSaving: (context) {
        final args = settings.arguments as Map<String, dynamic>?;
        return DaylightSavingPage(
          initialDate: args?['initialDate'] as String? ?? '2024-07-22',
          initialWeeks:
              args?['initialWeeks'] as List<String>? ??
              ['周一', '周二', '周三', '周四', '周五'],
        );
      },
    };
  }

  // Navigation methods
  static Future<DaylightSavingResult?> navigateToDaylightSaving(
    BuildContext context, {
    String? initialDate,
    List<String>? initialWeeks,
  }) async {
    final params = DaylightSavingParams(
      initialDate: initialDate ?? '2024-08-01',
      initialWeeks: initialWeeks ?? ['周一', '周三', '周五'],
    );

    final result =
        await Navigator.pushNamed(
              context,
              daylightSaving,
              arguments: params.toMap(),
            )
            as Map<String, dynamic>?;

    if (result == null) return null;
    return DaylightSavingResult.fromMap(result);
  }

  // Route generator
  static Route<dynamic> generateRoute(RouteSettings settings) {
    final routes = getRoutes(settings);
    final WidgetBuilder? builder = routes[settings.name];

    if (builder != null) {
      return MaterialPageRoute(builder: builder, settings: settings);
    }

    // If route not found, return to home
    return MaterialPageRoute(
      builder: (context) =>
          const Scaffold(body: Center(child: Text('Route not found'))),
    );
  }
}
